package com.ty.controller;

import com.ty.entity.PoItem;
import com.ty.entity.PoItemQuery;
import com.ty.entity.PoMaster;
import com.ty.entity.PoMasterQuery;
import com.ty.service.PoItemService;
import com.ty.service.PoMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//restfullApi
@RequestMapping("/api/pomaster")
//@CrossOrigin(origins = "*") // 解决跨域问题
//@NoWapperX
public class PoMasterController {

    @Autowired
    private PoMasterService poMasterService;

    @GetMapping// 查询 万能接口
    @PreAuthorize("hasAnyRole('ROLE_PURCHASE_MANGER','ROLE_PURCHASE')")
    public List<PoMaster> select (PoMasterQuery query) {
        //验证token

        return poMasterService.select(query);
    }

    @PostMapping
    public Integer insert(@RequestBody PoMaster poMaster){
        return poMasterService.insert(poMaster);
    }

    @PutMapping //从http协议的请求体
    public int update(@RequestBody PoMaster poMaster) {
        return  poMasterService.update(poMaster);
    }

    @GetMapping("/approve")
    @PreAuthorize("hasPermission(#res_id,'')")
    public int approve(@RequestParam String id,@RequestParam String res_id) {
        return  poMasterService.approve(id);
    }

    @GetMapping ("/reject")
    @PreAuthorize("hasPermission('PO_REJECT.BUTTON','PO_REJECT')")
    public int reject(@RequestParam String id) {
        return  poMasterService.reject(id);
    }

    @GetMapping ("/interrupt")
    @PreAuthorize("hasPermission(#id,'PO_INTERRUPT')")
    public int interrupt(@RequestParam String id) {
        return  poMasterService.interrupt(id);
    }

    @DeleteMapping //@RequestParam要求 url 上面一定要传参数 ?id=3
    public int delete(@RequestParam Integer id){
        return poMasterService.delete(id);
    }

}
